import prisma from '../prisma/index.js'
export const getTopicsService = async (name, skip, pagesize) => {
  try {
    return await prisma.topic.findMany({
      where: {
        name: {
          contains: name,
        },
      },
      include: {
        posts: true
      },
      skip,
      take: pagesize,
    })
  } catch (err) {
    throw err
  }
}

export const getTopicService = async id => {
  try {
    return await prisma.topic.findUnique({
      where: { id },
      include: {
        posts: true
      },
    })
  } catch (err) {
    throw err
  }
}

export const postTopicService = async data => {
  try {
    return await prisma.topic.create({
      data,
    })
  } catch (err) {
    throw err
  }
}

export const patchTopicService = async (id, data) => {
  try {
    return await prisma.topic.update({
      where: { id },
      data,
    })
  } catch (err) {
    throw err
  }
}

export const deleteTopicService = async id => {
  try {
    return await prisma.topic.deleteMany({
      where: { id },
    })
  } catch (err) {
    throw err
  }
}
